package tr.gov.tubitak.bilgem.esya.certselector;

import tr.gov.tubitak.bilgem.esya.certviewer.ECertViewerDialog;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
/*
 * Created by JFormDesigner on Thu Oct 13 15:35:01 EEST 2011
 */



/**
 * @author Haydar Karabas
 */
public class ReaderCertTree extends JTree {
    public ReaderCertTree() {
        initComponents();
    }

    public ReaderCertTree(DefaultMutableTreeNode iRootNode)
    {
        super(iRootNode);
        initComponents();
    }

    private void thisMouseClicked(MouseEvent e) {
        if (e.getClickCount() == 1) {
            return;
        }
        if (!e.isConsumed())
        {
            TreePath path = getClosestPathForLocation(e.getX(),e.getY());
            if(path != null)
            {
                Object lastComponent = path.getLastPathComponent();
                if(lastComponent instanceof DefaultMutableTreeNode)
                {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode)lastComponent;
                    Object userObject = node.getUserObject();
                    if(userObject instanceof CertInfo)
                    {
                        CertInfo certInfo =(CertInfo)userObject;
                        showCertificate(certInfo.geteCertViewerCert().getX509Cert());
                    }
                }
            }
        }
    }

    private void showCertificate(X509Certificate x509Cert) {
        try {
            ECertViewerDialog.showCertificate(x509Cert.getEncoded());
        } catch (CertificateEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            JOptionPane.showMessageDialog(null,CertSelectorBundle.getString("ReaderCertListTree.EncodingError")+e.getMessage());
        }
    }

    private void initComponents()
    {
        //setCellEditor(new ReaderCertListTreeNodeEditor(this));
        setCellRenderer(new ReaderCertListTreeRenderer());
        setEditable(false);
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        addMouseListener(new MouseAdapter() {
        //---- this ----
            @Override
            public void mouseClicked(MouseEvent e) {
                thisMouseClicked(e);
            }
        });
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

class ReaderCertListTreeRenderer extends DefaultTreeCellRenderer
{
    ReaderCertListTreeRenderer()
    {
    };

    public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus)
    {
        Component comp = super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
        //return comp;
        if(value instanceof EReaderListNode){
            EReaderListNode eReaderListNode = (EReaderListNode) value;
            if(eReaderListNode.isBusy()){
                setIcon(new AnimatedIcon(EImageManager.getInstance().getImageIcon("loading.gif", "Animation Icon")));
            }
            else
            {
                setIcon(EImageManager.getInstance().getImageIcon("CardReaderList_16.png","CardReaderList_16.png"));
            }
        }
        else if(value instanceof DefaultMutableTreeNode)
        {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
            Object userObject = treeNode.getUserObject();
            if(userObject instanceof ReaderInfo)
            {
                ReaderInfo readerInfo = (ReaderInfo)userObject;
                if(!readerInfo.ismIsLoaded())
                {
                    setIcon(new AnimatedIcon(EImageManager.getInstance().getImageIcon("loading.gif", "Animation Icon")));
                }
                else
                {
                    setIcon(EImageManager.getInstance().getImageIcon("CardReader_16.png","Certificate Icon"));
                }
            }
            else
            if(userObject instanceof CertInfo)
            {
                setIcon(EImageManager.getInstance().getImageIcon("SignerUser16.png","Certificate Icon"));
            }
        }
        return comp;
    };
}
